package com.yuyu.controller;

import com.yuyu.bean.PageData;
import com.yuyu.bean.PageDataOfMybatis;
import com.yuyu.bean.Product;
import com.yuyu.bean.ResultData;
import com.yuyu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailParseException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    //跳转到上传excel文件页面
    //上货页面跳转
    @RequestMapping("/toUploadExcelPage")
    public String toUploadExcelPage(){
        return "excel-upload-show";
    }

    //上货页面跳转
    @RequestMapping("/addInventory")
    public String addInventory(){
        return "add-inventory";
    }

    //批量删除
    @RequestMapping("/deleteProducts")
    @ResponseBody
    public Map deleteProducts(@RequestBody List<Integer> list){

        System.out.println("前台Ajax传递过来的参数: "+ list);


        int result = productService.deleteProduct(list);
        Map<String, Object> map = new HashMap<>();
        map.put("state_code", result);

        return map;

    }

    //通过id查询
    @RequestMapping("/findProductById")
    public String findProductById(Integer id, Model model){
        Product product = productService.findProductById(id);
        model.addAttribute("product", product);
        System.out.println(product);
        return "product-show";
    }

    //编辑页面获得信息
    @RequestMapping("/showProductById.edit.action")
    public String showProductById(Integer id, Model model){
        Product product = productService.findProductById(id);
        model.addAttribute("product", product);
        System.out.println(product);
        return "product-edit";
    }

    //通过条码查询
    @RequestMapping("/findProductByBarCode")
    @ResponseBody
    public Map findProductByBarCode(String code){
        Product product = productService.findProductByBarCode(code);
        System.out.println(product);
        Map<String,Object> map = new HashMap<>();
        if(product!=null){
            map.put("bar_code",product.getBar_code());
            map.put("product",product);
            map.put("state_code",1);
        }else {
            map.put("state_code",0);
        }

        return map;
    }

    //通过名称查询
    @RequestMapping("/findProductByName")
    public String findProductByName(String name, Model model){
        List<Product> products = productService.findProductByName(name);
        model.addAttribute("products", products);
        System.out.println(products);
        return "manager-product";
    }
    //通过类别查询
    @RequestMapping("/findProductByClassifyName")
    public String findProductByClassifyName(String name, Model model){
        List<Product> products = productService.findProductByClassifyName(name);
        model.addAttribute("products", products);
        System.out.println(products);
        return "manager-product";
    }

    //综合查询
    @RequestMapping("/findProductPageComprehensive")
    @ResponseBody
    public ResultData<List<Product>> findProductComprehensive(String condition, Integer index, Integer pageCount){
        System.out.println(condition);


        //计算页面信息
        PageData pageData = new PageData();
        pageData.setPageNumber(index);
        pageData.setPageLimit(pageCount);
        PageDataOfMybatis pageDataOfMybatis = PageDataOfMybatis.handle(pageData);

        //返回的结果集
        ResultData<List<Product>> resultData = new ResultData<>();
        List<Product> products = new ArrayList<>();
        List<Product> products_1 = productService.findProductByClassifyName(condition);
        products.addAll(products_1);
        List<Product> products_2 = productService.findProductByName(condition);
        if(products_2!=null){
            products.addAll(products_2);
        }
        Product product_2 = productService.findProductByBarCode(condition);
        if(product_2!=null){
            products.add(product_2);
        }
        //当没有条件时查询所有
        if(condition.equals("")){
            products = productService.findAllProduct();
        }
        //去重
        HashSet<Product> h = new HashSet<Product>(products);
        products.clear();
        products.addAll(h);
        System.out.println(h);
        //降序

        Collections.sort(products, new Comparator<Product>() {
            @Override
            public int compare(Product o1, Product o2) {
                return o2.getId() - o1.getId();

            }
        });

        //计算当前页
        //总数量
        int size = products.size();
        //开始位置
        int start = pageDataOfMybatis.getStartNum();
        //结束位置
        int end = pageDataOfMybatis.getStartNum() + pageCount;
        if (end >= size)
            end = size;
        System.out.println("start=" + start + ", end=" + end + ", size=" + size);

        //把查询到的数据添加到结果集中
        products = products.subList(start, end);
        resultData.setData(products);
        resultData.setCode(products.size());
        resultData.setMsg("综合查询结果");
        resultData.setCount(size);
        resultData.setAddtionData(pageCount);
        System.out.println(products);
        return resultData;
    }

    //添加

    //删除

    //查找全部商品
    @RequestMapping("/findAllProduct")
    public String findAllProduct(Model model){
        List<Product> products = productService.findAllProduct();
        model.addAttribute("products", products);
        System.out.println(products);
        return "manager-product";
    }

    @RequestMapping("/addProductPage")
    public String addProduct(){
        return "product-add";
    }

    //异步添加商品
    @RequestMapping("/addProduct")
    @ResponseBody
    public Map addProduct(Product product){
        int result = productService.addProduct(product);
        System.out.println(product);
        Map<String,Object> map = new HashMap<>();
        map.put("state_code",result);

        return map;
    }

    //异步修改商品信息
    @RequestMapping("/editProduct")
    @ResponseBody
    public Map editProduct(Product product){
        int result = productService.editProduct(product);
        System.out.println(product);
        Map<String,Object> map = new HashMap<>();
        map.put("state_code",result);

        return map;
    }

}
